
function editor_Initialize(editor,hiddenHtml) {

	   editor.document.designMode = 'on';	
	   editor.document.open();
	   editor.document.write(hiddenHtml.value.substring(1));
	   editor.document.close();
  	   editor.document.contentEditable = 'True';	
	   editor.document.body.style.margin="6 px";
	   editor.document.body.style.border="0";	
	   editor_ApplyEditorStyles(editor);
  }

 function editor_ApplyEditorStyles(editor) {
	  bs = editor.document.body.style;
	  bs.scrollbar3dLightColor= '#D4D0C8';
	  bs.scrollbarArrowColor= '#000000';
	  bs.scrollbarBaseColor= '#D4D0C8';
	  bs.scrollbarDarkShadowColor= '#D4D0C8';
	  bs.scrollbarFaceColor= '#D4D0C8';
 	  bs.scrollbarHighlightColor= '#808080';
	  bs.scrollbarShadowColor= '#808080';
	  bs.scrollbarTrackColor= '#D4D0C8';
	  bs.border='0';
  }
  
  function editor_ChangeMode(editor,tempframe,toolbarname,goToHtmlMode,autohide) {
	 var toolbar = document.getElementById(toolbarname);
	 var sTmp;
	 if(goToHtmlMode){
		sTmp=editor.document.body.innerHTML;				
		editor.document.body.style.fontFamily = 'Courier New';
		editor.document.body.style.fontSize = '10pt';
		if (toolbar != null && autohide) {
			toolbar.style.display = 'none';
		}
		editor.document.body.innerText=sTmp;
		return true;
	 }else{
		// go to Design Mode
		sTmp=editor.document.body.innerText;
		editor.document.body.style.fontFamily = '';
		editor.document.body.style.fontSize = '';
		if (toolbar != null && autohide) {
			toolbar.style.display = 'inline';
		}
		editor.document.body.innerHTML=sTmp;				
		editor_ApplyEditorStyles(editor);
		editor.focus(); 
		return true;
	} 
 }

function doPostBack(eventTarget, eventArgument) {
		var theform;
		if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
			theform = document.forms["announceForm"];
		}
		else {
			theform = document.announceForm;
		}
		theform.submit();
	}

function Strikethrough(editor,htmlmode) { 
	Format(editor,htmlmode,'strikethrough'); 
}

function WordClean(editor,htmlmode) {
	editor.focus();
	// 0bject based cleaning
	var body = editor.document.body;
	for (var index = 0; index < body.all.length; index++) {
		tag = body.all[index];
		//if (tag.Attribute["className"].indexOf("mso") > -1)
		tag.removeAttribute("className","",0);
		tag.removeAttribute("style","",0);
	}

	// Regex based cleaning
	var html = editor.document.body.innerHTML;
	html = html.replace(/<o:p>&nbsp;<\/o:p>/g, "");
	html = html.replace(/o:/g, "");
	//html = html.replace(/<st1:.*?>/g, "");

	// Final clean up of empty tags
	html = html.replace(/<font>/g, "");
	html = html.replace(/<span>/g, "");
	
	editor.document.body.innerHTML = html;

}

function SetFontSize(editor,htmlmode,name,value) {
	if (htmlmode) return;
	editor.focus();
	editor.document.execCommand('fontsize','',value);
}

function Redo(editor,htmlmode) {
	editor.focus();
 	editor.document.execCommand('redo','',null);
}

function editor_onKeyDown_ParagraphBreak() {
	editor = EditBox;
	var _TAB = 9;
	var _ENTER = 13;
	var _QUOTE = 222;
	var _OPENCURLY = '&#8220;';
	var _CLOSECURLY = '&#8221;';
	
	if (editor.event.keyCode == _TAB && editor.event.ctrlKey) {
		//HtmlMode to DesignMode (Ctrl+TAB)
		if (editor_HtmlMode && editor.event.keyCode == _TAB && editor.event.ctrlKey) {
			SetActiveTab(document.getElementById('editor_DesignModeTab'));
			editor_ChangeMode(editor,document.getElementById('editor_TempFrame'),'editor_Toolbar',false,true);
		}
		//DesignMode to HtmlMode (Ctrl+TAB)
		if (!editor_HtmlMode && editor.event.keyCode == _TAB && editor.event.ctrlKey) {
			SetActiveTab(document.getElementById('editor_HtmlModeTab'));
			editor_ChangeMode(editor,document.getElementById('editor_TempFrame'),'editor_Toolbar',true,true);
		}
			editor_HtmlMode = !editor_HtmlMode;
		
	}
	if (editor.event.keyCode == _QUOTE && editor.event.shiftKey && !editor_HtmlMode) {
		var sel = editor.document.selection;
		if (sel.type == 'Control') return;
		var r = sel.createRange();
		var before = CharBefore(r);
		var after = CharAfter(r);
		var r = sel.createRange();

		if (before == 'start') {
			r.pasteHTML(_OPENCURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		} else if (before != ' ' && after == 'end') {
			r.pasteHTML(_CLOSECURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		} else if (before == ' ' && after == 'end') {
			r.pasteHTML(_OPENCURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		} else if (before != ' ' && after == ' ') {
			r.pasteHTML(_CLOSECURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		} else {
			r.pasteHTML(_OPENCURLY);
			editor.event.cancelBubble = true;
			editor.event.returnValue = false;
			return false;
		}
	}

	if (editor.event.keyCode == _TAB) {
		editor.event.cancelBubble = true;
		editor.event.returnValue = false;
	}
	
	//check for Ctrl pressed.  Ctrl+Tab == switch modes.  Ctrl+Enter = <br>
	if ((editor_HtmlMode && editor.event.keyCode == _ENTER) || (!editor_HtmlMode && editor.event.ctrlKey && editor.event.keyCode == _ENTER)) {
		var sel = editor.document.selection;
		if (sel.type == 'Control') {
			return;
		}
		var r = sel.createRange();
		if ((!CheckTag(r.parentElement(),'LI'))&&(!CheckTag(r.parentElement(),'H'))) {
			r.pasteHTML('<br>');
			editor.event.cancelBubble = true; 
			editor.event.returnValue = false; 
			r.select();
			r.collapse(false);
			return false;
		}
	}
}

function Cut(editor,htmlmode) {
	editor.focus();
	editor.document.execCommand('cut','',null);
}

function InsertTime(editor,htmlmode) {
	editor.focus();
	var d = new Date();
	sel = editor.document.selection.createRange();
	sel.pasteHTML(d.toLocaleString());
}

function InsertDate(editor,htmlmode) {
	editor.focus();
	var d = new Date();
	sel = editor.document.selection.createRange();
	sel.pasteHTML(d.toLocaleString());
}

function InsertTable(editor,htmlmode) {
	if (htmlmode) return;
	editor.focus();

	var tablescript = HelperFilesPath + 'inserttable.html';
	if (HelperFilesParameters != '') tablescript += '?' + HelperFilesParameters;
	tableArr = showModalDialog(tablescript,window,'dialogWidth:350px; dialogHeight:222px;help:0;status:0;resizeable:1;');

	if (tableArr != null) {
		var newTable = editor.document.createElement('TABLE');
		for(y=0; y<tableArr['rows']; y++) {
			var newRow = newTable.insertRow();
			for(x=0; x<tableArr['cols']; x++) {
				var newCell = newRow.insertCell();				
				if (tableArr['valigncells'] != "") {
					newCell.valign = tableArr['valigncells'];
				}
				if (tableArr['haligncells'] != "") {
					newCell.align = tableArr['haligncells'];
				}				
				if (tableArr['percentcols'] == true) {					
					newCell.width = Math.round((1 / tableArr['cols']) * 100) + "%";
				}					
			}
		}
		newTable.border = tableArr['border'];
		newTable.cellspacing = tableArr['cellspacing'];
		newTable.cellpadding = tableArr['cellpadding'];		
		if (tableArr['width'] != "") newTable.width = tableArr['width'];
		if (tableArr['height'] != "") newTable.height = tableArr['height'];

		if (editor.document.selection.type=='Control') {
			sel.pasteHTML(newTable.outerHTML);
		} else {
			sel = editor.document.selection.createRange();
			sel.pasteHTML(newTable.outerHTML);
		}
	} else {
		//alert("You did not select an image");
	}
}

function Italic(editor,htmlmode) { 
	Format(editor,htmlmode,'italic'); 
}

function Copy(editor,htmlmode) {
	editor.focus();
	editor.document.execCommand('copy','',null);
}

function Outdent(editor,htmlmode) { 
	Format(editor,htmlmode,'outdent'); 
}

function CreateLink(editor,htmlmode) {
	if (htmlmode) return;
	editor.focus();
    editor.document.execCommand('createlink','1',null);
}

function Indent(editor,htmlmode) { 
	Format(editor,htmlmode,'indent'); 
}

function JustifyRight(editor,htmlmode) { 
	Format(editor,htmlmode,'justifyright'); 
}

function editor_onPaste() {}

function NumberedList(editor,htmlmode) { 
	Format(editor,htmlmode,'insertorderedlist'); 
}

function Save_editor(editor,htmlmode) { 
	CopyHtmlToHidden(editor,document.getElementById('hiddenContent'),htmlmode);	
	doPostBack('editor','Save')
}

function Subscript(editor,htmlmode) { 
	Format(editor,htmlmode,'subscript'); 
}

function ForeColorPicker(editor,htmlmode) {
	if (htmlmode) return;
	script = HelperFilesPath + 'colorpicker.html';
	if (HelperFilesParameters != '') script += '?' + HelperFilesParameters
	color = showModalDialog(script,window,'dialogWidth:210px;dialogHeight:170px;status:0;scroll:0;help:0;');
	editor.document.execCommand('forecolor','',color);
}

function Undo(editor,htmlmode) {
	editor.focus();
	editor.document.execCommand('undo','',null);
}

function InsertImageFromGallery_editor(editor,htmlmode) {
	if (htmlmode) return;
	editor.focus();

	obj = GetRangeReference(editor);
	if (obj.tagName == 'IMG') {
		editor.document.execCommand('insertimage',1,'');
		return;
	}

	var folder = 'images';
	var galleryscript = HelperFilesPath + 'ftb.imagegallery.aspx?rif='+folder+'&cif='+folder;
	if (HelperFilesParameters != '') galleryscript += '&' + HelperFilesParameters;
	imgArr = showModalDialog(galleryscript,window,'dialogWidth:560px; dialogHeight:500px;help:0;status:0;resizeable:1;');

	if (imgArr != null) {
		imagestring = '<IMG SRC="' + imgArr['filename'] + '" HEIGHT=' + imgArr['height'] + ' WIDTH=' + imgArr['width'] + ' BORDER=0>';
		sel = editor.document.selection.createRange();
		sel.pasteHTML(imagestring);
	} else {
		//alert("You did not select an image");
	}
}

function InsertRule(editor,htmlmode) { 
	Format(editor,htmlmode,'inserthorizontalrule'); 
}

function JustifyLeft(editor,htmlmode) { 
	Format(editor,htmlmode,'justifyleft'); 
}

function Paste(editor,htmlmode) {
	editor.focus();
	editor.document.execCommand('paste','',null);
}

// *******************************************
// Universal Editor Functions
// *******************************************
function CopyHtmlToHidden(editor,hiddenHtml,htmlmode) {
	if (!htmlmode) {
		hiddenHtml.value = "S"+editor.document.body.innerHTML;
	} else {
		hiddenHtml.value = "S"+editor.document.body.innerText; 
	}
	if (hiddenHtml.value == '<P>&nbsp;</P>') {
		hiddenHtml.value = '';
	}
	
}
function Format(editor,htmlmode,format) {
	if (htmlmode) return; 
	editor.focus();
	editor.document.execCommand(format,'',null);
}	
function CheckTag(item,tagName) {
	if (item.tagName.search(tagName)!=-1) {
		return item;
	}
	if (item.tagName=='BODY') {
		return false;
	}
	item=item.parentElement;
	return CheckTag(item,tagName);
}
function CharBefore(sel) {
	if (sel.move('character',-1) == -1) {
		sel.expand('character');
		return sel.text;
	} else {
		return 'start';
	}
}
function CharAfter(sel) {
	var sel2 = sel;
	if (sel.expand('character')) {
		sel2.move('character',1);
		sel2.expand('character');
		return sel2.text;
	} else {
		return 'end';
	}
}
function CharBefore(r) {
	if (r.move('character',-1) == -1) {
		r.expand('character');
		return r.text;
	} else {
		return 'start';
	}
}
function CharAfter(r) {
	var r2 = r;
	if (r.expand('character')) {
		r2.move('character',1);
		r2.expand('character');
		return r2.text;
	} else {
		return 'end';
	}
}
function GetRangeReference(editor) {
	editor.focus();
	var objReference = null;
	var RangeType = editor.document.selection.type;
	var selectedRange = editor.document.selection.createRange();

	switch(RangeType) {
		case 'Control' :
			if (selectedRange.length > 0 )  {
				objReference = selectedRange.item(0);
			}
			break;

		case 'None' :
			objReference = selectedRange.parentElement();
			break;

		case 'Text' :
			objReference = selectedRange.parentElement();
			break;
	}
	return objReference
}
// ********************************************
// Style Functions
// ********************************************
function SetButtonStyle(buttonTD,style,checkstyle) {
	if (buttonTD == null) return;
	if (buttonTD.className != checkstyle)
		buttonTD.className = style;
}
function GetClassSubName(className) {
	underscore = className.indexOf("_");
	if (underscore < 0) return className;
	return className.substring(underscore+1);
}
function ButtonOver(theTD,editorname,imageOver,imageDown) {
	SetButtonStyle(theTD,editorname+'_ButtonOver',null);
	if (eval(editorname+'_OverImage').src != '') theTD.background=eval(editorname+'_OverImage').src;
	
	if(theTD.children.length && theTD.children[0].tagName == "IMG" && imageOver){
		oldSrc = theTD.children[0].src;
		if (oldSrc.indexOf('.over.') == -1) {
			theTD.children[0].src=oldSrc.substring(0, oldSrc.length-4) + ".over.gif";
		}
	}
}
function ButtonOut(theTD,editorname,imageOver,imageDown) {
	SetButtonStyle(theTD,editorname+'_ButtonNormal',null);
	document.body.style.cursor = 'default';
	theTD.background='';
	if(theTD.children.length && theTD.children[0].tagName == "IMG"){
		oldSrc = theTD.children[0].src;
		if (oldSrc.indexOf('.over.') > 0) {
			theTD.children[0].src=oldSrc.substring(0, oldSrc.length-9) + ".gif";
		}
		if (oldSrc.indexOf('.down.') > 0) {
			theTD.children[0].src=oldSrc.substring(0, oldSrc.length-9) + ".gif";
		}
	}
}
function ButtonDown(theTD,editorname,imageOver,imageDown) {
	document.body.style.cursor = 'default';
	SetButtonStyle(theTD,editorname+'_ButtonDown',null);
	if (eval(editorname+'_DownImage').src != '') theTD.background=eval(editorname+'_DownImage').src;
	if(theTD.children.length && theTD.children[0].tagName == "IMG" && imageDown == 1){
		oldSrc = theTD.children[0].src;
		if (oldSrc.indexOf('.over.') > 0) {
			theTD.children[0].src=oldSrc.substring(0, oldSrc.length-9) + ".down.gif";
		}
	}
}
function ButtonUp(theTD,editorname,imageOver,imageDown) {
	document.body.style.cursor = 'auto';
	SetButtonStyle(theTD,editorname+'_ButtonOver',null);
	if (eval(editorname+'_OverImage').src != '') theTD.background=eval(editorname+'_OverImage').src;
	if(theTD.children.length && theTD.children[0].tagName == "IMG" && imageOver == 1){
		oldSrc = theTD.children[0].src;
		if (oldSrc.indexOf('.over.') == -1) {
			theTD.children[0].src=oldSrc.substring(0, oldSrc.length-4) + ".over.gif";
		}
	}
}
function SetActiveTab(theTD,editorname) {
	parentTR = theTD.parentElement;
	selectedTab = 1;
	totalButtons = parentTR.cells.length-1;
	for (var i=1;i< totalButtons;i++) {
		parentTR.cells[i].className = editorname + "_TabOffRight";
		if (theTD == parentTR.cells[i]) { selectedTab = i; }

	}

	if (selectedTab==1) {
		parentTR.cells[0].className = editorname + "_StartTabOn";
	} else {
		parentTR.cells[0].className = editorname + "_StartTabOff";
		parentTR.cells[selectedTab-1].className = editorname + "_TabOffLeft";
	}

	theTD.className = editorname + "_TabOn";
}
function TabOver() {
	document.body.style.cursor='default';
}
function TabOut() {
	document.body.style.cursor='auto';
}

function InsertSymbol(editor,htmlmode,name,value) {
	if (htmlmode) return;
	editor.focus();
	sel = editor.document.selection.createRange();
	sel.pasteHTML(value);
}

function Print(editor,htmlmode) { 
	editor.document.execCommand('print','',null); 
}

function BulletedList(editor,htmlmode) { 
	Format(editor,htmlmode,'insertunorderedlist'); 
}

function Unlink(editor,htmlmode) {
	if (htmlmode) return;
	editor.focus();
    editor.document.execCommand('unlink','1',null);
}

function SetParagraph(editor,htmlmode,name,value) {
	if (htmlmode) return;
	editor.focus();
	if (value == '<body>') {
		editor.document.execCommand('formatBlock','','Normal');
		editor.document.execCommand('removeFormat');
		return;
	}
	editor.document.execCommand('formatBlock','',value);
}

function SetFontFace(editor,htmlmode,name,value) {
	if (htmlmode) return;
	editor.focus();
	editor.document.execCommand('fontname','',value);
}

function Underline(editor,htmlmode) { 
	Format(editor,htmlmode,'underline'); 
}

function Superscript(editor,htmlmode) { 
	Format(editor,htmlmode,'superscript'); 
}

function Bold(editor,htmlmode) { 
	Format(editor,htmlmode,'bold'); 
}

function BackColorPicker(editor,htmlmode) {
	if (htmlmode) return;
	script = HelperFilesPath + 'colorpicker.html';
	if (HelperFilesParameters != '') script += '?' + HelperFilesParameters
	color = showModalDialog(script,window,'dialogWidth:210px;dialogHeight:170px;status:0;scroll:0;help:0;');
	editor.document.execCommand('backcolor','',color);
}

function JustifyFull(editor,htmlmode) { 
	Format(editor,htmlmode,'justifyfull'); 
}

function JustifyCenter(editor,htmlmode) { 
	Format(editor,htmlmode,'justifycenter'); 
}

function RemoveFormat(editor,htmlmode) { 
	Format(editor,htmlmode,'removeFormat'); 
}

img_ButtonOverImage = new Image();
img_ButtonOverImage.src = "images/editor/toolbarbutton.over.gif";
img_ButtonDownImage = new Image();
img_ButtonDownImage.src = "images/editor/toolbarbutton.down.gif";
img_Save = new Image();
img_Save.src = "images/editor/save.gif";
img_print = new Image();
img_print.src = "images/editor/print.gif";
img_undo = new Image();
img_undo.src = "images/editor/undo.gif";
img_redo = new Image();
img_redo.src = "images/editor/redo.gif";
img_wordclean = new Image();
img_wordclean.src = "images/editor/wordclean.gif";
img_inserttable = new Image();
img_inserttable.src = "images/editor/inserttable.gif";
img_fontforecolorpicker = new Image();
img_fontforecolorpicker.src = "images/editor/fontforecolorpicker.gif";
img_fontbackcolorpicker = new Image();
img_fontbackcolorpicker.src = "images/editor/fontbackcolorpicker.gif";
img_bold = new Image();
img_bold.src = "images/editor/bold.gif";
img_italic = new Image();
img_italic.src = "images/editor/italic.gif";
img_underline = new Image();
img_underline.src = "images/editor/underline.gif";
img_strikethrough = new Image();
img_strikethrough.src = "images/editor/strikethrough.gif";
img_superscript = new Image();
img_superscript.src = "images/editor/superscript.gif";
img_subscript = new Image();
img_subscript.src = "images/editor/subscript.gif";
img_removeformat = new Image();
img_removeformat.src = "images/editor/removeformat.gif";
img_justifyleft = new Image();
img_justifyleft.src = "images/editor/justifyleft.gif";
img_justifyright = new Image();
img_justifyright.src = "images/editor/justifyright.gif";
img_justifycenter = new Image();
img_justifycenter.src = "images/editor/justifycenter.gif";
img_justifyfull = new Image();
img_justifyfull.src = "images/editor/justifyfull.gif";
img_bullets = new Image();
img_bullets.src = "images/editor/bullets.gif";
img_numberedlist = new Image();
img_numberedlist.src = "images/editor/numberedlist.gif";
img_indent = new Image();
img_indent.src = "images/editor/indent.gif";
img_outdent = new Image();
img_outdent.src = "images/editor/outdent.gif";
img_createlink = new Image();
img_createlink.src = "images/editor/createlink.gif";
img_unlink = new Image();
img_unlink.src = "images/editor/unlink.gif";
img_insertimagefromgallery = new Image();
img_insertimagefromgallery.src = "images/editor/insertimagefromgallery.gif";
img_insertrule = new Image();
img_insertrule.src = "images/editor/insertrule.gif";
img_cut = new Image();
img_cut.src = "images/editor/cut.gif";
img_copy = new Image();
img_copy.src = "images/editor/copy.gif";
img_paste = new Image();
img_paste.src = "images/editor/paste.gif";
img_SpellCheck = new Image();
img_SpellCheck.src = "images/editor/spellcheck.gif";


